import { Injectable } from '@nestjs/common';
import { CreateCaseDto } from './dto/create-case.dto';
import { UpdateCaseDto } from './dto/update-case.dto';
import { PrismaService } from 'src/prisma/prisma.service';
import { Api } from 'src/utils/api';
import { API_CODES } from 'src/const/api.const';


@Injectable()
export class CasesService {

  constructor(private prisma: PrismaService) { }

  //创建操作
  async create(createCaseDto: CreateCaseDto) {
    // console.log("createCaseDto---", createCaseDto /;.);
    await this.prisma.testCases.create({
      data: createCaseDto
    });
  }

  //根据fid查询所有 
  async findAll(sid: number) {
    // const data = await this
    const data = await this.prisma.testCases.findMany({
      where: {
        sid
      },
      orderBy: {
        sort: 'asc'
      }
    });
    return Api.ok(data);
  }

  // 查询单个步骤
  async findOne(id: number) {
    const testCase = await this.prisma.testCases.findFirst({
      where: { id: id }
    });
    // 单个测试步骤
    if (testCase) {
      return Api.ok(testCase);
    } else {
      return Api.err(API_CODES.FEATURE_NO_EXIST);
    }
  }

  async updateSort(id:number, sort:number){
    return await this.prisma.testCases.update({
      where:{id},
      data:{sort}
    })
  }

  // 更新操作
  async update(id: number, updateCaseDto: UpdateCaseDto) {
    await this.prisma.testCases.update({
      where: { id },
      data: updateCaseDto
    });
    return Api.ok();
  }

  // 删除操作
  async remove(id: number) {
    await this.prisma.testCases.delete({
      where: { id: id }
    });
    return Api.ok();
  }

  async copy(id:number){
    const data = await this.prisma.testCases.findFirst({
      where:{id}
    });
    let  _newData = {...data}
    _newData.sort = data.sort+1;
    delete _newData.id;

    await this.prisma.testCases.create({
      data:_newData
    })
  }

}
